/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xuexiang.xuidemo.view;

import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.StackLayout;
import ohos.agp.components.Text;
import ohos.agp.utils.Color;
import ohos.app.Context;

import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.util.AttrValue;

import java.util.List;

/**
 * 下拉框菜单栏
 *
 * @since 2021-04-10
 */
public class DropDownMenu extends DirectionalLayout implements Component.ClickedListener {
    private Context context;
    private Text text1;
    private Text text2;
    private Text text3;
    private Text text4;
    private Text textid;
    private Image img1;
    private Image img2;
    private Image img3;
    private Image img4;
    private DependentLayout dplayout1;
    private DependentLayout dplayout2;
    private DependentLayout dplayout3;
    private DependentLayout dplayout4;
    private boolean isDix = true; // 判断箭头向下
    private boolean isLast = false; // 下拉框是否关闭标记
    private StackLayout stlayout1;
    private Color mBlue;
    private Color mColorwhile;
    private String mConstellation;
    private static final int anInt = -1;
    private static final int anInt1 = 1;
    private static final int anInt2 = 2;
    private static final int anInt3 = 3;
    private static final int anInt300 = 300;
    private static final int anInt500 = 500;
    private int post = anInt;
    private AnimatorProperty openproperty;
    private AnimatorProperty closeproperty;
    private Image image;
    private Text text;

    /**
     * 一个参数
     *
     * @param context 上下文
     */
    public DropDownMenu(Context context) {
        super(context);
        this.context = context;
        initview();
    }

    /**
     * 两个参数的构造方法
     *
     * @param context 上下文
     * @param attrSet 样式
     */
    public DropDownMenu(Context context, AttrSet attrSet) {
        super(context, attrSet);
        this.context = context;
        isDix = AttrValue.get(attrSet, "isDix", true);
        openproperty = new AnimatorProperty();
        closeproperty = new AnimatorProperty();
        initview();
    }

    /**
     * 三个参数的构造方法
     *
     * @param context 上下文
     * @param attrSet AttrSet
     * @param styleName string
     */
    public DropDownMenu(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        this.context = context;
        initview();
    }

    /**
     * 控件加载方法
     */
    private void initview() {
        mBlue = new Color(Color.getIntColor("#299EE3"));
        mColorwhile = new Color(Color.getIntColor("#FF7E7E7E"));
        Component ct = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_dropdownmeun_view, this, true);
        text1 = (Text) ct.findComponentById(ResourceTable.Id_text1);
        text2 = (Text) ct.findComponentById(ResourceTable.Id_text2);
        text3 = (Text) ct.findComponentById(ResourceTable.Id_text3);
        text4 = (Text) ct.findComponentById(ResourceTable.Id_text4);
        textid = (Text) ct.findComponentById(ResourceTable.Id_textid);
        img1 = (Image) ct.findComponentById(ResourceTable.Id_img1);
        img2 = (Image) ct.findComponentById(ResourceTable.Id_img2);
        img3 = (Image) ct.findComponentById(ResourceTable.Id_img3);
        img4 = (Image) ct.findComponentById(ResourceTable.Id_img4);
        dplayout1 = (DependentLayout) ct.findComponentById(ResourceTable.Id_dplayout1);
        dplayout2 = (DependentLayout) ct.findComponentById(ResourceTable.Id_dplayout2);
        dplayout3 = (DependentLayout) ct.findComponentById(ResourceTable.Id_dplayout3);
        dplayout4 = (DependentLayout) ct.findComponentById(ResourceTable.Id_dplayout4);
        stlayout1 = (StackLayout) ct.findComponentById(ResourceTable.Id_stlayout1);
        dplayout1.setClickedListener(this::onClick);
        dplayout2.setClickedListener(this::onClick);
        dplayout3.setClickedListener(this::onClick);
        dplayout4.setClickedListener(this::onClick);
        textid.setClickedListener(this::onClick);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_dplayout1:
                setPixelMap(img1, 0, text1);
                break;
            case ResourceTable.Id_dplayout2:
                setPixelMap(img2, anInt1, text2);
                break;
            case ResourceTable.Id_dplayout3:
                setPixelMap(img3, anInt2, text3);
                break;
            case ResourceTable.Id_dplayout4:
                setPixelMap(img4, anInt3, text4);
                break;
            case ResourceTable.Id_textid:
                closeMenu();
                break;
            default:
                break;
        }
    }

    /**
     * 关闭菜单栏
     */
    public void closeMenu() {
        if (isLast) { // 表示有打开的
            switch (post) {
                case 0:
                    img1.setPixelMap(ResourceTable.Media_icon_xjt);
                    text1.setTextColor(mColorwhile);
                    break;
                case anInt1:
                    img2.setPixelMap(ResourceTable.Media_icon_xjt);
                    text2.setTextColor(mColorwhile);
                    break;
                case anInt2:
                    img3.setPixelMap(ResourceTable.Media_icon_xjt);
                    text3.setTextColor(mColorwhile);
                    break;
                case anInt3:
                    img4.setPixelMap(ResourceTable.Media_icon_xjt);
                    text4.setTextColor(mColorwhile);
                    break;
                default:
                    break;
            }
            stlayout1.setVisibility(Component.HIDE);
            stlayout1.getComponentAt(post).setVisibility(Component.HIDE);
            this.post = anInt;
            this.isLast = false;
            this.isDix = true;
        }
    }

    /**
     * 对最后一个进行处理
     */
    public void setFourhide() {
        setPixelMap(img4, anInt3, text4);
        text4.setText(mConstellation);
    }

    private void setPixelMap(Image img, int pt, Text pltext) {
        if (post == anInt) { // 第一进来
            this.post = pt;
            isLast = true;
            setStartAnmation(stlayout1);
            stlayout1.setVisibility(Component.VISIBLE);
            stlayout1.getComponentAt(pt).setVisibility(Component.VISIBLE);
            img.setPixelMap(ResourceTable.Media_icon_sjt);
            pltext.setTextColor(mBlue);
        } else if (pt != post) { // 当前按得和上一次按得不一样
            openproperty.cancel();
            closeproperty.cancel();
            isDix = true;
            isLast = true;
            getpostImge().setPixelMap(ResourceTable.Media_icon_xjt);
            getpostText().setTextColor(mColorwhile);
            img.setPixelMap(ResourceTable.Media_icon_sjt);
            pltext.setTextColor(mBlue);
            setStartAnmation(stlayout1); // 加入动画为避免无效果出入
            stlayout1.getComponentAt(post).setVisibility(Component.HIDE);
            stlayout1.setVisibility(Component.VISIBLE);
            stlayout1.getComponentAt(pt).setVisibility(Component.VISIBLE);
            this.post = pt;
        } else if (pt == post) { // 按得相同位置,表示第二次按这个按钮
            if (isDix) { // 关闭
                isLast = false;
                isDix = false;
                img.setPixelMap(ResourceTable.Media_icon_xjt);
                pltext.setTextColor(mColorwhile);
                setStotAnmation(stlayout1);
            } else {
                isDix = true;
                isLast = true;
                setStartAnmation(stlayout1);
                img.setPixelMap(ResourceTable.Media_icon_sjt);
                pltext.setTextColor(mBlue);
                stlayout1.setVisibility(Component.VISIBLE);
                stlayout1.getComponentAt(pt).setVisibility(Component.VISIBLE);
            }
        }
    }

    /**
     * 获取上次的点击的图片
     *
     * @return img
     */
    private Image getpostImge() {
        switch (post) {
            case 0:
                image = img1;
                break;
            case anInt1:
                image = img2;
                break;
            case anInt2:
                image = img3;
                break;
            case anInt3:
                image = img4;
                break;
            default:
                break;
        }
        return image;
    }

    /**
     * 获得text文本
     *
     * @return text
     */
    private Text getpostText() {
        switch (post) {
            case 0:
                text = text1;
                break;
            case anInt1:
                text = text2;
                break;
            case anInt2:
                text = text3;
                break;
            case anInt3:
                text = text4;
                break;
            default:
                break;
        }
        return text;
    }

    /**
     * 加载控件
     *
     * @param list 加载控件的集合
     */
    public void setCompoent(List<Component> list) {
        for (int i = 0; i < list.size(); i++) {
            stlayout1.addComponent(list.get(i));
        }
    }

    /**
     * 设置文本
     *
     * @param pt 标题的第几个
     * @param content 上下文
     */
    public void setTabMenuText(int pt, String content) {
        this.post = pt;
        String name = "不限";
        switch (pt) {
            case 0:
                if (content.equals(name)) {
                    text1.setText("城市");
                } else {
                    text1.setText(content);
                }
                closeMenu();
                break;
            case anInt1:
                if (content.equals(name)) {
                    text2.setText("年龄");
                } else {
                    text2.setText(content);
                }
                closeMenu();
                break;
            case anInt2:
                if (content.equals(name)) {
                    text3.setText("性别");
                } else {
                    text3.setText(content);
                }
                closeMenu();
                break;
            case anInt3:
                if (content.equals(name)) {
                    this.mConstellation = "星座";
                } else {
                    this.mConstellation = content;
                }
                break;
            default:
                break;
        }
    }

    /**
     * 开始动画
     *
     * @param component 传入需要做动画的view
     */
    private void setStartAnmation(Component component) {
        closeproperty.cancel();
        if (component.getHeight() == 0) {
            openproperty.moveFromY(-AttrHelper.vp2px(anInt500, context)); // 开始距离y轴的距离
        } else {
            openproperty.moveFromY(-component.getHeight()); // 开始距离y轴的距离
        }
        openproperty.moveToY(0);
        openproperty.setTarget(component);
        openproperty.setDuration(anInt300);
        openproperty.start();
    }

    /**
     * 结束动画
     *
     * @param comt 传入需要做动画的view
     */
    private void setStotAnmation(Component comt) {
        openproperty.cancel();
        closeproperty.moveToY(-comt.getHeight()); // 结束距离位置
        closeproperty.setTarget(comt);
        closeproperty.setDuration(anInt300);
        closeproperty.start();
    }
}
